package zhuyun.code.datamanager.viewmodel

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import zhuyun.code.datamanager.model.Account
import zhuyun.code.datamanager.model.AfterAddCallback
import zhuyun.code.datamanager.model.MysqlDatabase
import zhuyun.code.datamanager.model.UpdateCallback

/**
 * @author: Administrator
 * @date: 2021/11/1
 */
class AccountViewModel(private val mysqlDatabase: MysqlDatabase):ViewModel() {

    private val _data = MutableLiveData<MutableList<Account?>>().apply { value = mutableListOf() }
    val data: MutableLiveData<MutableList<Account?>> = _data
    private val _addSuccess  = MutableLiveData<Boolean>()
    val addSuccess: MutableLiveData<Boolean> = _addSuccess

    fun loadData() {
        mysqlDatabase.loadData(object :UpdateCallback{
            override fun update(accounts: List<Account?>) {
                data.value = accounts.toMutableList()
            }

        })
    }
    fun addAccount(account:Account?){
        mysqlDatabase.addAccount(account, object :AfterAddCallback{
            override fun update(result: Boolean) {
                addSuccess.value = result
            }

        })
    }





}